/**************************************************************************************
 *  Copyright (c)  2010  Leszek Karcz, Changmin Bae.
 *  Permission is granted to copy, distribute and/or modify this document
 *  under the terms of the GNU Free Documentation License, Version 1.3
 *  or any later version published by the Free Software Foundation;
 *  with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 *  A copy of the license is included in the section entitled "GNU
 *  Free Documentation License". 
 **************************************************************************************/
package data.missions;
import data.*;
import data.tileables.Controllable;
import data.tileables.Rally;
import data.tileables.Unit;

/**************************************************************************************
 * Author(s): Changmin Bae
 * MoveToRally command
 **************************************************************************************/
public class MoveToRally extends Mission{
	private Map map;
	private Player player;
	private int rallyID;
	private int[] destCoords;
	private LinkedList<Integer> path;
	private LinkedList<Rally> rallyList;
	private Rally rally;
	private Unit thisControllable;
	private boolean isValidRally;
	private int ticksTilMove;
	/**************************************************************************************
	 * Author(s): Changmin Bae
	 * MoveToRally constructor
	 **************************************************************************************/
	public MoveToRally(Unit c, Map m, Player p, int rID)
	{
		isValidRally = false;
		destCoords = new int[2];
		map = m;
		player = p;
		rallyID = rID;
		thisControllable = c;
		rallyList = player.getAllRallies();

		if(!(rallyList.getSize() <= rallyID))
		{
			displayName = "Moving to Rally " + rID;
			isValidRally = true;
			rally = rallyList.peekIndex(rallyID);
			destCoords[0] = rally.getX();
			destCoords[1] = rally.getY();
			rally.addUnit(thisControllable);
			ticksTilMove = thisControllable.getMovementTicks();
		}
	}
	/**************************************************************************************
	 * Author(s): Changmin Bae
	 * MoveToRally execution
	 **************************************************************************************/
	public int execute()
	{
		if(isValidRally && ticksTilMove == 0)
		{
			ticksTilMove=thisControllable.getMovementTicks();
			if(rally.getX() != destCoords[0] && rally.getY() != destCoords[1])
			{
				path = null;
			}
			if(thisControllable.getX() == rally.getX() && thisControllable.getY() == rally.getY())
			{
				map.moveControllable(thisControllable, destCoords[0], destCoords[1], path);
				rally.addUnit(thisControllable);
				return 1;
			}
			if(path == null)
			{
				destCoords[0] = rally.getX();
				destCoords[1] = rally.getY();
				path = map.moveControllable(thisControllable, destCoords[0], destCoords[1], path);
				if(path == null)
				{
					return 1;
				}
				else
				{
					return 0;
				}
			}
			else if(path.getSize() == 0)
			{
				path = map.moveControllable(thisControllable, destCoords[0], destCoords[1], path);
				rally.addUnit(thisControllable);
				return 1;
			}
			else
			{
				path = map.moveControllable(thisControllable, destCoords[0], destCoords[1], path);
				rally.addUnit(thisControllable);
				return 0;
			}
		}
		ticksTilMove--;
		return 0;
	}

}
